<template>
	<view class="phraseList">
		<view class="content">
			<!-- <view class="tips">长按拖动可排序</view> -->
			<view class="vertical">
				<!-- <sortable :domId="verticalDomId" :options="{}" @changeList="verticalListChange"> -->
					<view class="list" id="vertical">
						<view class="item" v-for="(item,index) in phraseListAry" :key="index" :data-id="item.id" @longpress="vibrateShort">
							<view class="text">{{item.content}}</view>
							<view class="handleBtns">
								<view class="del" @click="openDelDialog(item.id)">
									<!-- <uni-icons type="trash"></uni-icons> -->
									删除
								</view>
								<view class="edit"  @click="handleJump('/im/phrase?type=edit&id=' + item.id + '&content=' + encodeURIComponent(item.content))">
									<!-- <uni-icons type="compose"></uni-icons> -->
									编辑
								</view>
							</view> 
						</view>
					</view> 
				<!-- </sortable> -->
			</view>
			<view class="btns btnShadow">
				<view class="btn" @click="addPhrase">
					添加常用语
				</view>
			</view>
		</view>
		<Popup ref="delDialog" title="系统提示" @confirm="handleDelPhrase">
			<view class="deleteParse">
				是否要删除该常用语？
			</view>
		</Popup>
	</view>
</template>

<script>
	import http from '@/utils/http'
	import api from '@/api'
	// import {delPhrase} from "@/utils/api.js"
	import { mapState } from "vuex"
	// import sortable from '@/components/dragSorts/cms-sortable-renderjs.vue'
	export default {
		data() {
			return {
				phraseListAry:[],
				verticalDomId:'',
				sort:false,
				delay:1000
			};
		},
		components: {
			// sortable
		},
		computed: {
			...mapState(['imToken'])
		},
		onShow() {
			this.getPhrase()
		},
		methods: {
			// 路由跳转
			handleJump(url){
				uni.navigateTo({
					url:url
				})
			},
			// 添加常用语
			addPhrase(){
				if(this.phraseListAry.length>=15){
					uni.showToast({
						title:'最多添加15条常用语',
						icon:"none"
					})
					return false
				} 
				uni.navigateTo({
					url:'/pages/im/components/phrase?type=add'
				})
				
			},
			// 获取常用语列表
			getPhrase(){
				http.post(api.phraseList,{
					token:this.imToken
				}).then(res=>{
					if(res.data.code == 200){
						let { items } = res.data.data
						this.phraseListAry = items
						this.verticalDomId = 'vertical';
					}
				})
			},
			// 列表长按
			vibrateShort() {
				uni.vibrateShort()
			},
			// 拖拽排序后数组
			verticalListChange(list){
				var sortList = []
				list.forEach((item,index)=>{
					this.phraseListAry.forEach((item2,index2)=>{
						if(item == item2.id){
							sortList.push(item2)
						}
					})
				})
				http.post(api.phraseSortAll,{
					token:this.imToken,
					sort_data:sortList
				}).then(res=>{
					if(res.data.code == 200){
						uni.showToast({
							title:res.message,
							icon:"none"
						})
					}
				})
			},
			// 是否排序
			handleIsSort(){
				// this.sort = !this.sort
				// if(this.sort){
				// 	this.delay = 1000
				// } else {
				// 	this.delay = 400
				// }
			},
			// 删除
			handleDelPhrase(){
				http.post(api.phraseDel,{token: this.imToken, id: this.DelId}).then(res=>{
					if(res.data.code == 200){
						this.getPhrase()
						uni.showToast({
							title:res.message,
							icon:"none"
						})
					}
				})
			},
			// 打开删除弹窗
			openDelDialog(id){
				this.DelId = id
				this.$refs.delDialog.open()
			}
		},
	}
</script>

<style lang="scss" scoped>
.phraseList{
	width: 100%;
	height: 100%;
	.sort{
		color:#333;
		font-size: 28rpx;
	}
	.title{
		color: #333;
		font-size: 30rpx;
		text-align: center;
		line-height: 80rpx;
	}
	.content{
		height: calc(100% - 160rpx);
		.tips {
			color: #AAAAAA;
			font-size: 28rpx; 
			padding: 21rpx 36rpx 0;
		}
		.vertical{
			height: 100%;
			padding: 0 36rpx;
			overflow-y: auto;
			.item{
				padding: 45rpx 0 20rpx;
				border-bottom: 1rpx solid #ededed;
				.text{
					color: #333;
					font-size: 28rpx;
					word-wrap: break-word;
					word-break: normal;
					word-break:break-all;
				}
				.handleBtns{
					display: flex;
					justify-content: flex-end;
					margin-top: 15rpx;
					.del{
						color:#333;
						font-size: 28rpx;
						text-align: right;
						padding-right: 30rpx;
					}
					.edit{
						color:#333;
						font-size: 28rpx;
						text-align: right;
						padding-right: 30rpx;
					}
				}
				
			}
		}
		.btns{
			background: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			padding: 30rpx 0;
			// border-top: 1rpx solid #ededed;
			.btn{
				width: 670rpx;
				height: 81rpx;
				background:#1787fb;
				border-radius: 6rpx;
				font-size: 28rpx;  
				text-align: center;
				line-height: 81rpx;
				color: #fff;
				margin: 0 auto;
			}
		}
	}
}
.deleteParse{
	margin: 20rpx auto 30rpx;
}
</style>
